//
//  RoommateApplyStateViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/4/8.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "RoommateApplyStateViewController.h"
#import "RoomMateTeamViewController.h"
#import "HouseDetailViewController.h"
#import "PersonCentreViewController.h"
#import "CheckSelfMessageViewController.h"
#import "RoommateApplyStateTableViewCell.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
#import "HttpServer.h"
#import "Utility.h"
#import "Constants.h"
#import "Apply.h"
#import "Seat.h"

@interface RoommateApplyStateViewController ()<UITableViewDataSource,UITableViewDelegate,RoommateApplyStateTableViewCellDelegate>
{
    NSMutableArray *applyDataArray;
}
@end

@implementation RoommateApplyStateViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    applyDataArray = [NSMutableArray array];
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(goToMain)];
    self.navigationItem.leftBarButtonItem = leftBar;
    self.title = @"申请状态";
    [_mainTableView registerNib:[UINib nibWithNibName:@"RoommateApplyStateTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"RoommateApplyStateTableViewCell"];
    _mainTableView.rowHeight = 173;

}
-(void)viewWillAppear:(BOOL)animated
{
    [HttpServer getApplyStateWithUserId:[Utility getObjectForKey:kUserId] WithSuccessBlock:^(NSDictionary *responseData)
    {
        NSLog(@"寝室申请状态＝＝%@",responseData);
        applyDataArray = [NSMutableArray array];
        for (NSDictionary *dic in responseData[@"applys"])
        {
            Apply *apply = [[Apply alloc]initWithDic:dic];
            [applyDataArray addObject:apply];
            NSLog(@"%lu",(unsigned long)apply.teamSeatArray.count);
        }
        NSLog(@"%lu",(unsigned long)applyDataArray.count);
        [_mainTableView reloadData];
    }
    withFailBlock:^(id errorMsg)
    {
        NSLog(@"%@",errorMsg);
    }];
}
-(void)viewWillDisappear:(BOOL)animated
{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}
-(void)goToMain
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return applyDataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Apply *apply = applyDataArray[indexPath.section];
    RoommateApplyStateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RoommateApplyStateTableViewCell"forIndexPath:indexPath];
    [cell.houseImageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:apply.houseCover] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Non_HP"]];
    cell.joinLabel.text = [NSString stringWithFormat:@"%@申请加入",apply.createTime];
    cell.houseNameLabel.text = apply.houseCommunity;
    cell.delegate = self;
    if ([apply.teamStatus isEqualToString:@"failed"])
    {
        cell.applyStateLabel.text = @"已失效";
        cell.applyStateLabel.backgroundColor = [UIColor colorWithRed:187.0/255 green:187.0/255 blue:187.0/255 alpha:1];
        [cell.noteButton setTitle:@"您申请的小组以失效，去浏览其它报价吧！" forState:UIControlStateNormal];
        cell.noteButton.tag = 0;
    }
    else
    {
        if ([apply.status isEqualToString:@"doing"])
        {
            cell.applyStateLabel.text = @"申请中";
            cell.applyStateLabel.backgroundColor = [UIColor colorWithRed:255.0/255 green:162.0/255 blue:0 alpha:1];
            [cell.noteButton setTitle:@"申请中，去完善个人信息通过率更高哦" forState:UIControlStateNormal];
            cell.noteButton.tag = 1;
        }
        else if ([apply.status isEqualToString:@"pass"])
        {
            cell.applyStateLabel.text = @"已通过";
            cell.applyStateLabel.backgroundColor = [UIColor colorWithRed:0 green:187.0/255 blue:55.0/255 alpha:1];
            [cell.noteButton setTitle:@"已通过，可在室友小组中查看" forState:UIControlStateNormal];
            cell.noteButton.tag = 2;
        }
        else if ([apply.status isEqualToString:@"reject"])
        {
            cell.applyStateLabel.text = @"被忽略";
            cell.applyStateLabel.backgroundColor = [UIColor colorWithRed:0 green:168.0/255 blue:242.0/255 alpha:1];
            [cell.noteButton setTitle:@"您的申请已被忽略，去完善个人信息通过率更高哦" forState:UIControlStateNormal];
            cell.noteButton.tag = 1;
        }
    }
    NSLog(@"apply.teamSeatArray.count%lu",(unsigned long)apply.teamSeatArray.count);
    switch (apply.teamSeatArray.count)
    {
        case 1:
        {
            cell.room_A_Label.text = ((Seat*)apply.teamSeatArray[0]).roomName;
            cell.room_A_PriceLabel.text = ((Seat*)apply.teamSeatArray[0]).leader == YES ? @"室长" : [NSString stringWithFormat:@"%d",((Seat*)apply.teamSeatArray[0]).rent];
        }
            break;
        case 2:
        {
            cell.room_A_Label.text = ((Seat*)apply.teamSeatArray[0]).roomName;
            cell.room_A_PriceLabel.text = ((Seat*)apply.teamSeatArray[0]).leader == YES ? @"室长" : [NSString stringWithFormat:@"%d",((Seat*)apply.teamSeatArray[0]).rent];
            
            cell.room_B_Label.text = ((Seat*)apply.teamSeatArray[1]).roomName;
            cell.room_B_PriceLabel.text = ((Seat*)apply.teamSeatArray[1]).leader == YES ? @"室长" : [NSString stringWithFormat:@"%d",((Seat*)apply.teamSeatArray[1]).rent];
        }
            break;
        case 3:
        {
            cell.room_A_Label.text = ((Seat*)apply.teamSeatArray[0]).roomName;
            cell.room_A_PriceLabel.text = ((Seat*)apply.teamSeatArray[0]).leader == YES ? @"室长" : [NSString stringWithFormat:@"%d",((Seat*)apply.teamSeatArray[0]).rent];
            cell.room_B_Label.text = ((Seat*)apply.teamSeatArray[1]).roomName;
            cell.room_B_PriceLabel.text = ((Seat*)apply.teamSeatArray[1]).leader == YES ? @"室长" : [NSString stringWithFormat:@"%d",((Seat*)apply.teamSeatArray[1]).rent];
            cell.room_C_Label.text = ((Seat*)apply.teamSeatArray[2]).roomName;
            cell.room_C_PriceLabel.text = ((Seat*)apply.teamSeatArray[2]).leader == YES ? @"室长" : [NSString stringWithFormat:@"%d",((Seat*)apply.teamSeatArray[2]).rent];
        }
            break;
        default:
            break;
    }
    for (int i = 0; i < apply.teamSeatArray.count; i ++)
    {
        NSLog(@"\n%@\n%@",((Seat*)apply.teamSeatArray[0]).userId,apply.seatId);
        if (i == 0)
        {
            if ([((Seat*)apply.teamSeatArray[0]).seatId isEqualToString:apply.seatId])
            {
                cell.room_A_PriceLabel.textColor = [UIColor colorWithRed:255.0/255 green:118.0/255 blue:108.0/255 alpha:1];
            }
            else
            {
                cell.room_A_PriceLabel.textColor = [UIColor colorWithRed:71.0/255 green:62.0/255 blue:58.0/255 alpha:1];
            }
        }
        else if(i == 1)
        {
            if ([((Seat*)apply.teamSeatArray[1]).seatId isEqualToString:apply.seatId])
            {
                cell.room_B_PriceLabel.textColor = [UIColor colorWithRed:255.0/255 green:118.0/255 blue:108.0/255 alpha:1];
            }
            else
            {
                cell.room_B_PriceLabel.textColor = [UIColor colorWithRed:71.0/255 green:62.0/255 blue:58.0/255 alpha:1];
            }
        }
        else
        {
            if ([((Seat*)apply.teamSeatArray[2]).seatId isEqualToString:apply.seatId])
            {
                cell.room_C_PriceLabel.textColor = [UIColor colorWithRed:255.0/255 green:118.0/255 blue:108.0/255 alpha:1];
            }
            else
            {
                cell.room_C_PriceLabel.textColor = [UIColor colorWithRed:71.0/255 green:62.0/255 blue:58.0/255 alpha:1];
            }
        }
        
    }
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HouseDetailViewController *houseDetailVC = [[HouseDetailViewController alloc]init];
    houseDetailVC.houseId = ((Apply*)(applyDataArray[indexPath.section])).houseId;
    [self.navigationController pushViewController:houseDetailVC animated:YES];
}

-(void)clickNoteButton:(UIButton *)sender
{
    NSLog(@"%ld",(long)sender.tag);
    switch (sender.tag)
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            //tiaozhuan wanshanzhiliao
            PersonCentreViewController *personCenterVC = [[PersonCentreViewController alloc]init];
            [self.navigationController pushViewController:personCenterVC  animated:YES];
        }
            break;
        default:
        {
            //tiaozhuan shiyou xiaozu
            RoomMateTeamViewController *roomTeamVC = [[RoomMateTeamViewController alloc]init];
            [self.navigationController pushViewController:roomTeamVC animated:YES];
        }
            break;
    }
}
/*
#pragma mark - Navigation

 In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     Get the new view controller using [segue destinationViewController].
     Pass the selected object to the new view controller.
}
*/

@end
